package com.exsun.ex1077.network.response

import com.exsun.ex1077.extend.logout
import com.exsun.ex1077.ui.activity.login.LoginActivity
import com.zhangping.fastjetpack.extend.finishToActivity
import com.zhangping.fastjetpack.extend.showToast
import com.zhangping.fastjetpack.network.BaseResponse

/**
 * 描述：API接口返回数据基类
 * 作者：ZhangPing
 * 创建时间：2022/2/23 21:34
 */
open class ApiResponse<T>(val code: Int, val msg: String = "", val data: T) : BaseResponse<T>() {

    override fun getResponseCode() = code

    override fun getResponseData() = data

    override fun getResponseMsg() = msg

    override fun isSuccess(): Boolean{
        if (code == 0 || code == 200){
            return true
        }
        if (code == 401){
            //Token失效，重新登录
            logout(true)
            finishToActivity<LoginActivity>()
        }
        showErrorMsg()
        return false
    }

    /**
     * 显示错误提示
     */
    open fun showErrorMsg(){
        getResponseMsg().showToast()
    }
}
